package com.topnav.mc.admin.entity.wrapper;

import com.topnav.mc.admin.entity.param.DeviceParam;
import com.topnav.mc.admin.entity.param.GbServerParam;
import com.topnav.mc.base.mc.McWrapper;
import org.springframework.util.StringUtils;

public class DeviceWrapper extends McWrapper<DeviceParam> {
    public DeviceWrapper(DeviceParam entity) {
        super(entity);
        if (this.permissionGrade != null ){
            if (this.permissionGrade.getPermissionLevel() == 1){
                this.eq("d.user_id",this.permissionGrade.getUserId());
            }else if (this.permissionGrade.getPermissionLevel() == 2){
                this.eq("d.org_id",this.permissionGrade.getOrgId());
            }else if (this.permissionGrade.getPermissionLevel() == 3 || this.permissionGrade.getPermissionLevel() == 4){
                this.likeRight("o.path",this.permissionGrade.getOrgPath());
            }
        }
        if (entity != null){
            if (entity.getId()!=null){this.eq("d.id",entity.getId());}
            if (!StringUtils.isEmpty(entity.getCode())){this.like("d.code",entity.getCode());}
            if (!StringUtils.isEmpty(entity.getName())){this.like("d.name",entity.getName());}
            if (!StringUtils.isEmpty(entity.getDefaultChannel())){this.like("d.default_channel",entity.getDefaultChannel());}
            if (entity.getType() != null){this.eq("d.type",entity.getType());}
            if (entity.getOrgId() != null){this.eq("d.org_id",entity.getOrgId());}
            if (entity.getUserId() != null){this.eq("d.user_id",entity.getUserId());}
            if (entity.getGbServerId() != null){this.eq("d.gb_server_id",entity.getGbServerId());}
            if (entity.getOnline()!=null){this.eq("d.online",entity.getOnline());}
            if (entity.getState()!=null){this.eq("d.state",entity.getState());}
            if (entity.getCate()!=null){this.eq("d.cate",entity.getCate());}

            if (entity.getSynch() != null){
                {this.eq("d.synch",entity.getSynch());}
            }
            if (!StringUtils.isEmpty(entity.getKeyword())){
                this.and(wrapper -> wrapper.like("d.code", entity.getKeyword())
                        .or().like("d.name", entity.getKeyword())
                        .or().like("d.default_channel", entity.getKeyword())
                );
            }

            if (!StringUtils.isEmpty(entity.getTalkAccount())){
                if (entity.getTalkAccount().indexOf(",") > -1){
                    this.in("d.talk_account",entity.getTalkAccount());
                }else{
                    this.eq("d.talk_account",entity.getTalkAccount());
                }

            }
            this.orderByDesc("d.id");

        }
    }
}
